{% extends "base.html" %}

{% block extrahead %} 

	{% if addMoreButton %}

		  <script type="text/javascript">
		    <!--
				function cloneMore(selector, type) {
				
				    var newElement = $(selector).clone(true);
				    var total = $('#id_' + type + '-TOTAL_FORMS').val();
				    newElement.find(':input').each(function() {
				        var name = $(this).attr('name').replace('-' + (total-1) + '-','-' + total + '-');
				        var id = 'id_' + name;
				
						if ( $(this).attr('name').match(/-id$/)  ) {
							$(this).removeAttr('value');
						}

				        if ($(this).attr('type') != 'hidden' && $(this).attr('type') != 'radio' && !$(this).attr('readonly')  ) {
				            $(this).val('');
				        }
				        if ($(this).attr('type') == 'checkbox' ) {
				            $(this).removeAttr('value');
						}				         
				        $(this).attr({'name': name, 'id': id}).removeAttr('checked');
				    });
				    newElement.find('label').each(function() {
				        var newFor = $(this).attr('for').replace('-' + (total-1) + '-','-' + total + '-');
				        $(this).attr('for', newFor);
				    });
				    newElement.find('.infoForm').remove();
				    
				    total++;
				    $('#id_' + type + '-TOTAL_FORMS').val(total);
				    $(selector).after(newElement);
			
					$(".timepicker").datepicker("destroy");
					$(".datepicker").datepicker("destroy");
					
					setPickers();

				}
				
		  //-->
		    </script>
	{% endif %}
{% endblock %}

{% block head %}
  {{ head }}
{% endblock %}

{% block content  %}
{% block preform  %}
	{% if missatge %}
		<div class="info">
		{{missatge}}
		</div>
	{% endif %}
	
	{% if formset.errors %}
		<div class="error">
			Atenció! S'han trobat errors al processar el formulari.
		</div>	
	{% endif %}
	
{% endblock %}
<form method="post" enctype="multipart/form-data" action="">{% csrf_token %}
    {{ formset.management_form }}
    {% for form in formset %}    
<div class='table'>
<table class="form">
        {%if form.formSetDelimited %} 
           <tr style="background-color:black; height='10'"><td colspan='2' style="background-color:black; height='10'">
           </td></tr> 
        {% endif %}       
  					
        {% if form.non_field_errors %}
		 <tr> <td colspan="2" >
		  <div class="conte_errors">
		  <ul style="margin: 0px">
	      {% for error in form.non_field_errors %}
	      	<li>{{error}}</li>
	      {% endfor %}
	      <ul>
	      </div>
	     </td></tr>
	    {% endif %}	
	    
		{% for label, value in form.infoForm %}
		<tr class="infoForm">
		    <td class="label_tag">
		    	{{ label }}:
		    </td>
		    <td>       
		        {{ value }}
		    </td>
		</tr>
		{% endfor %}		
			
	    <tr style="height='0'"><td colspan='2' height='0'>    
	    {% for hidden in form.hidden_fields %}
           
                {{ hidden }}
        {% endfor %}
        </td></tr> 
        
        {% for field in form.visible_fields %}
        
			<tr>		
			    <td class="label_tag">{{ field.label_tag }}:</td>
			    <td class="tria_tag" {% if field.errors %} class="conte_errors"  {% endif %}  >		        
			        {{ field }}
			        {% if field.help_text %}
			        <br/><span class="help_text">( {{ field.help_text }} )</span>
			        {% endif %}
			    	{{ field.errors }}
			    </td>
			</tr>
        {% endfor %}
        {%if formSetDelimited %} <tr style='background-color:white; height='10'><td colspan='2' style='background-color:white; height='10'></td></tr> {% endif %}       
</table>
</div>
    {% endfor %}
    {% if formset %}
    <table class="form"><tr><td colspan="2">
    
			<input type="submit" value="{% if msg_submit %}{{ msg_submit }}{% else %}Enviar dades{% endif %}" style="float:left;">
			
			{% if addMoreButton %}
			<input type="button" value="Afegir-ne més" id="add_more"  style="float:right;">
			<script>
		    $('#add_more').click(function() {
		        cloneMore('div.table:last', 'form');
		    });
		    {%endif%}
			</script>
	</td></tr></table>
	{% endif %}
</form>
{% block postform  %}

{% endblock %}
{% endblock %}


